package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.NewsAdvertisement;
import com.base.service.LogsService;
import com.base.service.NewsAdvertisementService;
import com.base.utils.IP;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_xiugaixinwenguanggao {
	@Autowired
	private NewsAdvertisementService newsAdvertisementService;
	@Autowired
	private LogsService logsService;// log日志
	
	// 用户首页
	@RequestMapping("/content_xiugaixinwenguanggao")
	public ModelAndView index(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		NewsAdvertisement newsAdvertisement = newsAdvertisementService.selectByPrimaryKey(id);
		if(newsAdvertisement != null) {
			
			request.setAttribute("newsAdvertisement", newsAdvertisement);
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_xiugaixinwenguanggao");
			return modelAndView;
		}
		
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	String SUCCESS = "success";
	@RequestMapping(value = "/updateNewsAdvertisement", method = RequestMethod.POST)
	public void updateNewsAdvertisement(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, Byte datastatus, String url,
			String text, String pic, Byte status) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
//		String parent = (String) request.getSession().getAttribute("adminUserId");
		
		NewsAdvertisement newsAdvertisement = newsAdvertisementService.selectByPrimaryKey(id);

		PrintWriter out = response.getWriter();
			if (id == null) {
				out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
				return;
			} else {
				newsAdvertisement.setUrl(url);
				newsAdvertisement.setText(text);
				newsAdvertisement.setPic(pic);
				newsAdvertisement.setDatastatus(datastatus);
				newsAdvertisement.setStatus(status);
				int code = newsAdvertisementService.updateByPrimaryKeySelective(newsAdvertisement);
				
				String usernamesys = (String) request.getSession().getAttribute("useracc");
                 
				if (code == 1) {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改新闻广告成功", usernamesys,
							true, IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改新闻广告失败", usernamesys,
							true, IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
		}
	}
	
	@RequestMapping(value = "/updatePosition", method = RequestMethod.POST)
	public void updateNewsAdvertisement(HttpServletRequest request, HttpServletResponse response,
			Integer position) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		List<NewsAdvertisement> newsAdvertisement = newsAdvertisementService.selectByAll(adminUserId);

		PrintWriter out = response.getWriter();
			if (position == null) {
				out.print("参数不能为空");
				return;
			} else {
				int code = 0;
				for(NewsAdvertisement n : newsAdvertisement) {
					n.setPosition(position);
					code = newsAdvertisementService.updateByPrimaryKeySelective(n);
				}
				
				String usernamesys = (String) request.getSession().getAttribute("useracc");
                 
				if (code == 1) {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改新闻广告成功", usernamesys,
							true, IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改新闻广告失败", usernamesys,
							true, IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
		}
	}
}
